package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.ShOrderDao;
import com.shop.shoppingmall.dao.ShProductCommentDao;
import com.shop.shoppingmall.dao.ShUserDao;
import com.shop.shoppingmall.entity.ShProductComment;
import com.shop.shoppingmall.entity.ShUser;
import com.shop.shoppingmall.service.ShProductCommentService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品评论表(ShProductComment)表服务实现类
 *
 * @author makejava
 * @since 2020-09-07 17:09:35
 */
@Service("shProductCommentService")
@Transactional
public class ShProductCommentServiceImpl implements ShProductCommentService {
    @Resource
    private ShProductCommentDao shProductCommentDao;
    @Resource
    private ShUserDao shUserDao;
    @Resource
    private ShOrderDao shOrderDao;

    /**
     * 通过ID查询单条数据
     *
     * @param commentId 主键
     * @return 实例对象
     */
    @Override
    public ShProductComment queryById(String commentId) {
        return this.shProductCommentDao.queryById(commentId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ShProductComment> queryAllByLimit(int offset, int limit) {
        return this.shProductCommentDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param shProductComment 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(ShProductComment shProductComment) {
        shProductComment.setCommentId(UUIDGenerator.generate());
        shProductCommentDao.insert(shProductComment);
        shOrderDao.updateCommentAndOrderStatus(shProductComment.getUserId(),shProductComment.getOrderId());
        return Result.ok("评论成功");
    }

    /**
     * 修改数据
     *
     * @param shProductComment 实例对象
     * @return 实例对象
     */
    @Override
    public ShProductComment update(ShProductComment shProductComment) {
        this.shProductCommentDao.update(shProductComment);
        return this.queryById(shProductComment.getCommentId());
    }

    /**
     * 通过主键删除数据
     *
     * @param commentId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String commentId) {
        return this.shProductCommentDao.deleteById(commentId) > 0;
    }

    /**
     * 展示该用户的所有的评价
     * @param request
     * @return
     */
    @Override
    public Result selectCommentByUserId(HttpServletRequest request) {
        //解析token，拿到userId
        String name = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(name);
        if(StringUtils.isBlank(name) || null == shUser){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        String userId = shUser.getId();
        List<ShProductComment> shProductComments = shProductCommentDao.selectCommentByUserId(userId);
        if(shProductComments.size() == 0 || shProductComments == null){
            return Result.error("您还没有发布过任何评论！");
        }
        return Result.ok(shProductComments);
    }

    /**
     * 查询该商品的所有的评论
     * @param productId
     * @return
     */
    @Override
    public Result selectCommentAll(String productId) {
        List<ShProductComment> shProductComments = shProductCommentDao.selectCommentAll(productId);
        if(shProductComments.size() == 0 || shProductComments == null){
            return Result.error("该商品暂时还没有任何评论！");
        }
        return Result.ok(shProductComments);
    }

    @Override
    public Result updateCommentStatus() {
        shProductCommentDao.updateCommentStatus();
        return Result.ok("修改成功！");
    }
}
